<template>
  <div class="top-bak">
    <h1 class="title">友链如絮</h1>
    <p class="subtitle">这些小小的链接，是散落网络的星光，以微光交织成桥，让每一次点击，都成为一场不期而遇的美好。</p>
  </div>
  <div class="friend-links-container">
    <!-- 友链分类标题 -->
    <div v-for="category in categories" :key="category" class="category-section">
      <h2 class="category-title" :style="{ color: categoryColors[category] }">
        {{ category }}
        <span class="category-count">({{ getCategoryCount(category) }})</span>
      </h2>

      <!-- 友链网格 -->
      <div class="links-grid">
        <a-card
            v-for="link in getLinksByCategory(category)"
            :key="link.id"
            class="link-card"
            hoverable
            :bordered="false"
            @click="handleLinkClick(link.url)"
        >
          <div class="link-card-content">
            <div class="link-logo">
              <img :src="link.logo" :alt="link.name" class="logo-img"/>
            </div>
            <div class="link-info">
              <div class="link-name">{{ link.name }}</div>
              <div class="link-desc">{{ link.description }}</div>
            </div>
          </div>
        </a-card>
      </div>
    </div>
  </div>
</template>

<script setup>
import {ref, computed, onMounted} from 'vue';
import {findFriendLinkList} from "@/api/friendLink.js";
// 友链数据
const friendLinks = ref([

]);

// 获取所有分类
const categories = computed(() => {
  const cats = new Set();
  friendLinks.value.forEach(link => cats.add(link.category));
  return Array.from(cats);
});

// 分类颜色映射
const categoryColors = {
  '技术': '#1890ff',
  '设计': '#722ed1',
  '生活': '#23c6c8',
  '艺术': '#fa8c16',
  '文化': '#f5222d',
  '工具': '#1890ff',
};

// 根据分类获取友链
const getLinksByCategory = (category) => {
  return friendLinks.value.filter(link => link.category === category);
};

// 获取分类友链数量
const getCategoryCount = (category) => {
  return getLinksByCategory(category).length;
};

// 处理友链点击
const handleLinkClick = (url) => {
  window.open(url, '_blank');
};

onMounted(() => {
  findFriendLinkList().then(res => {
    if (res.code === 200) {
      friendLinks.value = res.data;
    }
  }).catch(err => {
    console.log(err);
  }).finally(() => {

  })
})
</script>

<style scoped lang="less">
.top-bak {
  width: 100%;
  height: 350px;
  background: url('@/assets/img/rainbow-lollipop.jpg') center/cover no-repeat;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: #38b363;
  text-align: center;
  position: relative;
  margin-bottom: 50px;
}

.friend-links-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px;
  background-color: #f9fafb;
}

/* 分类部分 */
.category-section {
  margin-bottom: 40px;
}

.category-title {
  font-size: 20px;
  font-weight: 500;
  margin-bottom: 16px;
  padding-bottom: 8px;
  border-bottom: 2px solid;
  display: inline-block;
}

.category-count {
  margin-left: 8px;
  font-size: 14px;
  font-weight: normal;
  opacity: 0.8;
  background-color: rgba(0, 0, 0, 0.05);
  padding: 2px 8px;
  border-radius: 12px;
}

/* 友链网格 */
.links-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}

.link-card {
  transition: all 0.3s ease;
  height: 100%;
  border-radius: 10px !important;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.06);
  background-color: #fff;
}

.link-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
}

.link-card-content {
  display: flex;
  padding: 16px;
  gap: 16px;
  align-items: center;
}

.link-logo {
  width: 50px;
  height: 50px;
  flex-shrink: 0;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.08);
  background-color: #f0f2f5;
}

.logo-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.link-card:hover .logo-img {
  transform: scale(1.08);
}

.link-info {
  flex-grow: 1;
  min-width: 0;
}

.link-name {
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: #1d2129;
}

.link-desc {
  font-size: 14px;
  color: #666;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.5;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .friend-links-container {
    padding: 16px;
  }

  .links-grid {
    grid-template-columns: 1fr;
  }

  .category-title {
    font-size: 18px;
  }
}
</style>
